﻿namespace EFS.AD
{
	/// <summary>
	/// Ad user
	/// </summary>
	public class User
	{
		/// <summary>
		/// Constructor
		/// </summary>
		public User()
		{
		}

		/// <summary>
		/// Constructor
		/// </summary>
		public User(
			System.Guid objectGuid,
			System.String logonName,
			System.String emailAddress, 
			System.String sip,
			System.String tel,
			System.String firstName,
			System.String lastName,
			System.String description)
		{
			// set
			this.ObjectGuid = objectGuid;
			this.EmailAddress = emailAddress;
			this.LogonName = logonName;
			this.SIP = sip;
			this.FirstName = firstName;
			this.LastName = lastName;
			this.Description = description;

			// check tel
			if (EFS.Common.Verify.String(tel))
			{
				// check length (keep only last 4 digits) 
				this.TEL = tel; 
				if (tel.Length >= 4)
				{
					this.EXT = System.String.Format("ext={0}", tel.Substring(tel.Length - 4));
				}
			}
		}

		/// Property ObjectGuid: Get, Set
		public System.Guid ObjectGuid { get; set; }

		/// Property LogonName: Get, Set
		public System.String LogonName { get; set; }

		/// Property EmailAddress: Get, Set
		public System.String EmailAddress { get; set; }

		/// Property SIP: Get, Set
		public System.String SIP { get; set; }

		/// Property TEL: Get, Set
		public System.String TEL { get; set; }

		/// Property TEL: Get, Set
		public System.String EXT { get; set; }

		/// Property FirstName: Get, Set
		public System.String FirstName { get; set; }

		/// Property LastName: Get, Set
		public System.String LastName { get; set; }

		/// Property Description: Get, Set
		public System.String Description { get; set; }

		/// Property LastName: Get, Set
		public System.String FullName { get { return System.String.Format("{0} {1}", FirstName, LastName); } }

		/// Match
		private static System.Boolean Match(
			System.String source,
			System.String match,
			System.Boolean caseSensitive)
		{
			// prepare
			System.String sourceTemp = caseSensitive ? EFS.Common.Functions.String.GetTrimmedString(source) : EFS.Common.Functions.String.GetLowerTrimmedString(source);
			System.String matchTemp = caseSensitive ? EFS.Common.Functions.String.GetTrimmedString(match) : EFS.Common.Functions.String.GetLowerTrimmedString(match);

			// test match
			if (!EFS.Common.Verify.String(matchTemp))
			{
				return true;
			}

			// test source
			if (!EFS.Common.Verify.String(sourceTemp))
			{
				return false;
			}

			// ok
			return sourceTemp.Contains(matchTemp);
		}

		/// Match
		public System.Boolean Match(
			System.String partialLogonName,
			System.Boolean? hasSIP,
			System.String partialName,
			System.Boolean caseSensitive)
		{
			// ok
			return
				// LogonName
				(!EFS.Common.Verify.String(partialLogonName) || Match(LogonName, partialLogonName, caseSensitive)) &&
				(
					// FirstName
					(!EFS.Common.Verify.String(partialName) || Match(FirstName, partialName, caseSensitive)) ||
					// LastName
					(!EFS.Common.Verify.String(partialName) || Match(LastName, partialName, caseSensitive))
				) &&
				// SIP
				(!hasSIP.Value || ((hasSIP.Value && EFS.Common.Verify.String(SIP)) || (!hasSIP.Value && !EFS.Common.Verify.String(SIP))));
		}
	}

}